#!/bin/sh
# - monitor_repo
# - job_yaml
# - job_testbox

. $LKP_SRC/lib/log.sh

. $LKP_SRC/lib/job.sh
load_job_vars GIT_SERVER lab my_name my_email my_token SCHED_HOST SCHED_PORT

set_submit_config()
{
	mkdir -p ~/.config/compass-ci/defaults/
	cat > ~/.config/compass-ci/defaults/"$lab".yaml <<-EOF
		SCHED_HOST: $SCHED_HOST
		SCHED_PORT: $SCHED_PORT
		lab: $lab
	EOF

	cat > ~/.config/compass-ci/defaults/account.yaml <<-EOF
		my_name: $my_name
		my_email: $my_email
	EOF

	my_token=${my_token:-$secrets_my_token}
	mkdir -p ~/.config/compass-ci/include/lab/
	cat > ~/.config/compass-ci/include/lab/"$lab".yaml <<-EOF
		my_token: $my_token
	EOF
}

install_lkp()
{
	git clone git://$GIT_SERVER/gitee.com/compass-ci/lkp-tests.git "$LKP_SRC"
	cd "$LKP_SRC" && make install
}

download_monitor_repo()
{
	git clone git://"$GIT_SERVER"/"$monitor_repo" "$repo_path"
	cp -r "$repo_path"/* "$LKP_SRC"/
}

submit_job()
{
	if [ -f "$LKP_SRC"/"$job_yaml" ]; then
		job_yaml="$LKP_SRC"/"$job_yaml"
	else
		job_yaml=${job_yaml##*/}
	fi

	"$LKP_SRC"/sbin/submit -a "$job_yaml" testbox="$job_testbox"
}

main()
{
	[ -z "$monitor_repo" ] && log_error "monitor_repo is not specified" && exit 1

	local repo=${monitor_repo##*/}
	local repo_path=/c/"$repo"
	local LKP_SRC=/c/lkp-tests

	set_submit_config
	install_lkp
	download_monitor_repo
	submit_job
}

main
